package com.detection.dahua.service.impl;

import com.dahuatech.hutool.http.Method;
import com.dahuatech.hutool.json.JSONUtil;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.model.v202010.OauthConfigUserPwdInfo;
import com.dahuatech.icc.oauth.utils.HttpUtils;
import com.detection.dahua.config.OauthConfigUtil;
import com.detection.dahua.model.accesscontrol.record.QueryRecordCountResponse;
import com.detection.dahua.model.accesscontrol.record.QueryRecordRequest;
import com.detection.dahua.model.accesscontrol.record.QueryRecordResponse;
import com.detection.dahua.service.IDahuaAccessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class DahuaAccessServiceImpl implements IDahuaAccessService {

    /**
     * 门禁记录分页查询
     * @param queryRecordRequest
     * @return
     */
    @Override
    public QueryRecordResponse getRecordPage(QueryRecordRequest queryRecordRequest){
        OauthConfigUserPwdInfo config = OauthConfigUtil.getOauthConfig();
        QueryRecordResponse response=null;
        try {
            log.info("DeviceDemo,getRecordPage,request:{}", JSONUtil.toJsonStr(queryRecordRequest));
            response = HttpUtils.executeJson("/evo-apigw/evo-accesscontrol/1.0.0/card/accessControl/swingCardRecord/bycondition/combined", queryRecordRequest,null, Method.POST , config, QueryRecordResponse.class);
            log.info("DeviceDemo,getRecordPage,response:{}", JSONUtil.toJsonStr(response));
        } catch (ClientException e) {
            log.error(e.getErrMsg(), e);
        }
        if(!response.isSuccess()) {
            log.info("门禁记录分页查询失败:{}",response.getErrMsg());
        }
        return response;
    }

    /**
     * 门禁记录数量查询
     */
    public Integer getRecordCount(QueryRecordRequest queryRecordRequest){
        OauthConfigUserPwdInfo config = OauthConfigUtil.getOauthConfig();
        QueryRecordCountResponse response=null;
        try {
            log.info("RecordDemo,getRecordCount,request:{}", JSONUtil.toJsonStr(queryRecordRequest));
            response = HttpUtils.executeJson("/evo-apigw/evo-accesscontrol/1.0.0/card/accessControl/swingCardRecord/bycondition/combinedCount", queryRecordRequest,null, Method.POST , config, QueryRecordCountResponse.class);
            log.info("RecordDemo,getRecordCount,response:{}", JSONUtil.toJsonStr(response));
        } catch (ClientException e) {
            log.error(e.getErrMsg(), e);
        }
        if(!response.isSuccess()) {
            log.info("门禁记录数量查询失败:{}",response.getErrMsg());
        }
        Integer num=response.getData();
        log.info("门禁记录数量:{}",num);
        return num;
    }

    /**
     * 远程开门
     */
    @Override
    public Integer controlDoorOpen(List<String> list){
        OauthConfigUserPwdInfo config = OauthConfigUtil.getOauthConfig();
        QueryRecordCountResponse response=null;
        try {
            log.info("RecordDemo,getRecordCount,request:{}", JSONUtil.toJsonStr(list));
            response = HttpUtils.executeJson("/evo-apigw/evo-accesscontrol/1.0.0/card/accessControl/channelControl/openDoor", list,null, Method.POST , config, QueryRecordCountResponse.class);
            log.info("RecordDemo,getRecordCount,response:{}", JSONUtil.toJsonStr(response));
        } catch (ClientException e) {
            log.error(e.getErrMsg(), e);
        }
        if(!response.isSuccess()) {
            log.info("远程开门失败:{}",response.getErrMsg());
        }
        return 1;
    }

    /**
     * 远程关门
     */
    @Override
    public Integer controlDoorClose(List<String> list){
        OauthConfigUserPwdInfo config = OauthConfigUtil.getOauthConfig();
        QueryRecordCountResponse response=null;
        try {
            log.info("RecordDemo,getRecordCount,request:{}", JSONUtil.toJsonStr(list));
            response = HttpUtils.executeJson("/evo-apigw/evo-accesscontrol/1.0.0/card/accessControl/channelControl/closeDoor", list,null, Method.POST , config, QueryRecordCountResponse.class);
            log.info("RecordDemo,getRecordCount,response:{}", JSONUtil.toJsonStr(response));
        } catch (ClientException e) {
            log.error(e.getErrMsg(), e);
        }
        if(!response.isSuccess()) {
            log.info("远程关门失败:{}",response.getErrMsg());
        }
        return 1;
    }

    /**
     * 设置为常开模式
     */
    @Override
    public Integer controlDoorAlwaysOpen(List<String> list){
        OauthConfigUserPwdInfo config = OauthConfigUtil.getOauthConfig();
        QueryRecordCountResponse response=null;
        try {
            log.info("RecordDemo,getRecordCount,request:{}", JSONUtil.toJsonStr(list));
            response = HttpUtils.executeJson("/evo-apigw/evo-accesscontrol/1.0.0/card/accessControl/channelControl/stayOpen", list,null, Method.POST , config, QueryRecordCountResponse.class);
            log.info("RecordDemo,getRecordCount,response:{}", JSONUtil.toJsonStr(response));
        } catch (ClientException e) {
            log.error(e.getErrMsg(), e);
        }
        if(!response.isSuccess()) {
            log.info("控制门禁常开失败:{}",response.getErrMsg());
        }
        return 1;
    }

    /**
     * 设置为常关模式
     */
    @Override
    public Integer controlDoorAlwaysClose(List<String> list){
        OauthConfigUserPwdInfo config = OauthConfigUtil.getOauthConfig();
        QueryRecordCountResponse response=null;
        try {
            log.info("RecordDemo,getRecordCount,request:{}", JSONUtil.toJsonStr(list));
            response = HttpUtils.executeJson("/evo-apigw/evo-accesscontrol/1.0.0/card/accessControl/channelControl/stayClose", list,null, Method.POST , config, QueryRecordCountResponse.class);
            log.info("RecordDemo,getRecordCount,response:{}", JSONUtil.toJsonStr(response));
        } catch (ClientException e) {
            log.error(e.getErrMsg(), e);
        }
        if(!response.isSuccess()) {
            log.info("控制门禁常关失败:{}",response.getErrMsg());
        }
        return 1;
    }

}
